﻿using System;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.KDT;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;

namespace Company.Interface
{
    public partial class ToKhaiMauDichSendForm : BaseForm
    {
        public ToKhaiMauDichSendForm()
        {
            InitializeComponent();
        }

        private ToKhaiMauDichCollection tkmdCollection = new ToKhaiMauDichCollection();
        
        private void khoitao_DuLieuChuan()
        {
            this._LoaiHinhMauDich = LoaiHinhMauDich.SelectAll();
            this._Nuoc = Nuoc.SelectAll();
            this._DonViHaiQuan = DonViHaiQuan.SelectAll();
        }
        
        private void ToKhaiMauDichSendForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();
            display_ToKhaiMauDich_By_MaHaiQuanTiepNhan(ctrDonViHaiQuan.Ma);
        }

        private void display_ToKhaiMauDich_By_MaHaiQuanTiepNhan(string maHQTN)
        {
            string where = string.Format("MaHaiQuan = '{0}' AND SoTiepNhan = 0", maHQTN.PadRight(6));
            this.tkmdCollection = new ToKhaiMauDich().SelectCollectionDynamic(where, "");
            dgList.DataSource = this.tkmdCollection;
            dgList.Refetch();
        }

        private ToKhaiMauDich search(object id)
        {
            foreach (ToKhaiMauDich tkmd in this.tkmdCollection)
            {
                if (tkmd.ID == Convert.ToInt64(id))
                {
                    return tkmd;
                }
            }
            return null;
        }
        
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                GridEXSelectedItemCollection items = dgList.SelectedItems;
                if (items.Count == 0) return;
                WSForm wsForm = new WSForm();
                wsForm.ShowDialog(this);
                if (!wsForm.IsReady) return;
                foreach (GridEXSelectedItem i in items)
                {
                    if (i.RowType == RowType.Record)
                    {
                        ToKhaiMauDich tkmd = this.search(i.GetRow().Cells["ID"].Value);
                        long soTiepNhan = tkmd.WSSend();
                        if (soTiepNhan > 0)
                            ShowMessage("Khai báo tờ khai mậu dịch thành công.\nSố tiếp nhận: " + soTiepNhan, false);
                        else
                        {
                            ShowMessage("Khai báo không thành công.", false);
                        }
                    }

                }
                this.display_ToKhaiMauDich_By_MaHaiQuanTiepNhan(ctrDonViHaiQuan.Ma);
            }
            catch (Exception ex)
            {
                if (ShowMessage("Khai báo không thành công.\nLỗi: " + ex.Message + ".\nBạn có muốn đưa vào hàng đợi không?", true) == "Yes")
                {
                    ToKhaiMauDich tkmd = (ToKhaiMauDich)dgList.GetRow().DataRow;
                    HangDoi hd = new HangDoi();
                    hd.ID = tkmd.ID;
                    hd.LoaiToKhai = LoaiToKhai.TO_KHAI_MAU_DICH;
                    hd.TrangThai = TrangThaiXuLy.CHUA_KHAI_BAO;
                    hd.ChucNang = ChucNang.KHAI_BAO;
                    MainForm.AddToQueueForm(hd);
                    MainForm.ShowQueueForm();
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            this.display_ToKhaiMauDich_By_MaHaiQuanTiepNhan(ctrDonViHaiQuan.Ma);
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells["MaLoaiHinh"].Text = this.LoaiHinhMauDich_GetName(e.Row.Cells["MaLoaiHinh"].Value.ToString());
            e.Row.Cells["NuocNK_ID"].Text = this.Nuoc_GetName(e.Row.Cells["NuocNK_ID"].Value.ToString());
            e.Row.Cells["NuocXK_ID"].Text = this.Nuoc_GetName(e.Row.Cells["NuocXK_ID"].Value.ToString());
        }

        private ToKhaiMauDich getTKMDByID(long id)
        {
            foreach (ToKhaiMauDich tk in this.tkmdCollection)
            {
                if (tk.ID == id) return tk;
            }
            return null;
        }
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                long id = Convert.ToInt64(e.Row.Cells["ID"].Value);
                ToKhaiMauDichForm f = new ToKhaiMauDichForm();
                f.OpenType = OpenFormType.Edit;
                f.TKMD = this.getTKMDByID(id);                
                f.ShowDialog();
            }
        }

        private void dgList_DeletingRecord(object sender, RowActionCancelEventArgs e)
        {
            if (ShowMessage("Bạn có muốn xóa tờ khai này không", true) == "Yes")
            {
                if (e.Row.RowType == RowType.Record)
                {
                    long id = Convert.ToInt64(e.Row.Cells["ID"].Value);
                    ToKhaiMauDich tkmd = new ToKhaiMauDich();
                    tkmd.ID = id;
                    tkmd.Delete();
                }
            }
        }
    }
}